package com.example.lyl.yuejian.view.shop;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;
import com.example.lyl.yuejian.view.HomeActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class GoodsOrderActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.goods_iv)
    ImageView goodsIv;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.price_tv)
    TextView priceTv;
    @BindView(R.id.adress_tv)
    TextView adressTv;
    @BindView(R.id.jian_iv)
    ImageView jianIv;
    @BindView(R.id.geshu_tv)
    TextView geshuTv;
    @BindView(R.id.jia_iv)
    ImageView jiaIv;
    @BindView(R.id.hjje_tv)
    TextView hjjeTv;
    @BindView(R.id.tjdd_btn)
    Button tjddBtn;

    String name;
    int price;
    private int uid;
    private int sid;
    private int count = 1;
    private int countPrice = 0;
    String pic;
    private int oid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_order);
        ButterKnife.bind(this);
        toolbar.setTitle("提交订单");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        Intent intent = getIntent();
        sid = intent.getIntExtra("sid", 0);
        uid = intent.getIntExtra("uid", 0);
        price = intent.getIntExtra("price", 0);
        name = intent.getStringExtra("name");
        pic = intent.getStringExtra("pic");
        nameTv.setText(name);
        priceTv.setText("价格:  " + price + "");
        adressTv.setText("地址:  " + SharePreferenceUtils.getString(this, "adress", ""));
        geshuTv.setText(count + "");
        countPrice = count * price;
        hjjeTv.setText(countPrice + "");
        Glide.with(this).load(pic).into(goodsIv);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick({R.id.jian_iv, R.id.jia_iv, R.id.tjdd_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.jian_iv:
                if (count > 1) {
                    count--;
                    geshuTv.setText(count+"");
                    hjjeTv.setText("合计金额:  " + count * price + "");
                }
                break;
            case R.id.jia_iv:
                count++;
                geshuTv.setText(count+"");
                hjjeTv.setText("合计金额:  " + count * price + "");
                break;
            case R.id.tjdd_btn:
                NetTool.getInstance().getApi()
                        .addOrder(uid, sid, count)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<ResultBean>() {
                            @Override
                            public void accept(ResultBean resultBean) throws Exception {
                                Object data = resultBean.getData();
                                float oidF = Float.parseFloat(data.toString());
                                oid = (int)oidF;
                                Log.d("GoodsOrderActivity", "oid:" + oid);
                                Toast.makeText(GoodsOrderActivity.this, resultBean.getMessage(), Toast.LENGTH_SHORT).show();
                                new AlertDialog.Builder(GoodsOrderActivity.this).setTitle("确定支付" + price + "元")
                                        .setPositiveButton("付款", new DialogInterface.OnClickListener() {
                                            @Override


                                            public void onClick(DialogInterface dialog, int which) {
                                                NetTool.getInstance().getApi()
                                                        .payOrder(oid)
                                                        .subscribeOn(Schedulers.io())
                                                        .observeOn(AndroidSchedulers.mainThread())
                                                        .subscribe(new Consumer<ResultBean>() {
                                                            @Override
                                                            public void accept(ResultBean resultBean) throws Exception {
                                                                startActivity(new Intent(GoodsOrderActivity.this, HomeActivity.class));
                                                                Toast.makeText(GoodsOrderActivity.this, "购买成功", Toast.LENGTH_SHORT).show();
                                                            }
                                                        }, new Consumer<Throwable>() {
                                                            @Override
                                                            public void accept(Throwable throwable) throws Exception {
                                                                Toast.makeText(GoodsOrderActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                                                            }
                                                        });
                                            }
                                        })
                                        .setNegativeButton("再想想", null).create().show();

                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                throwable.printStackTrace();
                            }
                        });
                break;
        }
    }
}
